import 'package:hrf_app/common/extension/ex_visibility.dart';
import 'package:hrf_app/common/models/bug/file_model.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import '../api/http_head.dart';

class TextInputImageEdit extends StatelessWidget {
  /// 标题
  final String labelText;

  /// 自定义参数 在回调时会原封不动的返回去
  final dynamic customParam;

  /// 是否显示编辑按钮  (editParm && isShowEdit)
  final bool isShowAdd;

  ///编辑文字
  final String addText;

  /// 第一个时customParam 的内容
  final String? Function(dynamic)? addCallBack;
  final bool isShowDel;

  ///删除是回调
  final String? Function(dynamic)? delCallBack;

  /// 编辑的权限串 (editParm && isShowEdit)
  final String delParm;

  /// 编辑的权限串 (editParm && isShowEdit)
  final String addParm;

  final Color labelTextColor;

//是否显示必填写 *
  final bool isMustBeEnter;
  final List<UploadFileModel> uploadFileModelList;

  /// 字体大小
  final double size;

  /// padding
  final EdgeInsets? linePadding;
  final BoxDecoration? boxDecoration;

  //  构造函数
  const TextInputImageEdit(
      {Key? key,
      required this.labelText,
      this.customParam = "",
      this.isMustBeEnter = false,
      this.size = 16,
      this.isShowAdd = false,
      this.isShowDel = false,
      this.addText = "添加",
      this.delParm = "",
      this.addCallBack,
      this.delCallBack,
      this.labelTextColor = Colors.black54,
      required this.uploadFileModelList,
      this.boxDecoration = const BoxDecoration(color: Colors.white),
      this.linePadding = const EdgeInsets.all(8),
      this.addParm = ""})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
          decoration: boxDecoration, padding: linePadding, child: _buildLeft()),
      Container(
          decoration: boxDecoration,
          padding: linePadding,
          child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [_buildRight()]))
    ]);
  }

  Widget _buildLeft() {
    String mustBeEnter = isMustBeEnter ? "*" : " ";
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Container(
                margin: EdgeInsets.only(top: 4),
                child: Text(
                  mustBeEnter,
                  style: TextStyle(color: Colors.red, fontSize: size - 2),
                )),
            Text(
              labelText,
              style: TextStyle(fontSize: size, color: labelTextColor),
            ),
          ],
        ),
        GestureDetector(
            onTap: () {
              addCallBack!(customParam);
            },
            behavior: HitTestBehavior.opaque,
            excludeFromSemantics: false,
            dragStartBehavior: DragStartBehavior.start,
            child: Text(
              addText,
              style: TextStyle(
                fontSize: size - 2,
                color: Colors.blue,
              ),
            ).hasPermAndVisible(hasPerm: addParm, visible: isShowAdd)),
      ],
    );
  }

  Widget _buildRight() {
    return Expanded(child: _buildFileUpload());
  }

//附件上传
  Widget _buildFileUpload() {
    Widget fildUpload = Container(
        margin: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(3),
        ),
        child: Column(
          children: [
            Container(
                margin: EdgeInsets.symmetric(vertical: 5),
                child: GridView(
                  ///子Item排列规则
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                      //横轴元素个数
                      crossAxisCount: 3,
                      //纵轴间距
                      mainAxisSpacing: 2.0,
                      //横轴间距
                      crossAxisSpacing: 2.0,
                      //子组件宽高长度比例
                      childAspectRatio: 1),

                  ///GridView中使用的子Widegt
                  children: buildListViewItemList(),
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                )),
          ],
        ));
    return fildUpload;
  }

  List<Widget> buildListViewItemList() {
    List<Widget> itemList = [];
    for (var i = 0; i < uploadFileModelList.length; i++) {
      UploadFileModel itemVo = uploadFileModelList[i];
      Widget item = GestureDetector(
        child: Container(
          color: Colors.transparent,
          alignment: Alignment.center,
          child: Stack(alignment: Alignment.topRight, children: <Widget>[
            SizedBox(
              child: TDImage(
                width: 110,
                height: 110,
                imgUrl: HttpHead.minioUrl() + itemVo.url!,
                fit: BoxFit.cover,
              ),
            ),
            GestureDetector(
              child: Icon(TDIcons.close_circle_filled),
              onTap: () {
                delCallBack!(i);
              },
            ).hasPermAndVisible(hasPerm: delParm, visible: isShowDel),
          ]),
        ),
        onTap: () {
          imgView(i);
        },
      );
      itemList.add(item);
    }
    return itemList;
  }

  imgView(index) {
    List<String> imageList = [];
    for (var vo in uploadFileModelList) {
      imageList.add(HttpHead.minioUrl()+ vo.url!);
    }
    TDImageViewer.showImageViewer(
        context: Get.context!,
        images: imageList,
        showIndex: true,
        deleteBtn: false,
        defaultIndex: index);
  }
}
